﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telmex;

namespace Confree
{
    /// <summary>
    /// Window for creating a new conference.
    /// </summary>
    public partial class ConferenceWindow : Form
    {
        /// <summary>
        /// The desired conference parameters.
        /// </summary>
        public Conference HintConference { get; set; }

        /// <summary>
        /// If not null, the just created conference.
        /// </summary>
        public Conference CreatedRoom { get; set; }

        /// <summary>
        /// Instantiates a new conference window.
        /// </summary>
        public ConferenceWindow()
        {
            InitializeComponent();

            // Load the participants options
            comboParticipants.Items.AddRange(new object[] { 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            comboTolerance.SelectedIndex = 0;
        }


        public void SetRoomInfo(Conference conference)
        {
            // Save the hint
            HintConference = conference;

            // Set the conference description
            labelConferenceInfo.Text = String.Format("Create a room {0} that starts on {1} with a duration of {2} hours ({3} minutes).",
                !String.IsNullOrWhiteSpace(conference.Name) ? "named \"" + conference.Name + "\"" : "without name",
                conference.StartsOn.ToString("yyyy/MM/dd HH:mm"), 
                conference.Duration.ToString(@"hh\:mm"),
                conference.Duration.TotalMinutes.ToString());

            // Set the default participants number by the hint
            if (comboParticipants.Items.Contains(conference.Participants))
                comboParticipants.SelectedItem = conference.Participants;
            else
                comboParticipants.SelectedItem = conference.Participants < 2 ? 2 : 10;
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            // Get the selected participants number
            var tolerance = TimeSpan.Parse((string)comboTolerance.SelectedItem);
            HintConference.Participants = (int)comboParticipants.SelectedItem;
            HintConference.StartsOn = HintConference.StartsOn.Subtract(tolerance);
            HintConference.Duration = HintConference.Duration.Add(tolerance);
            // Call the Telmex service
            var api = new TelmexService();
            api.Login();
            CreatedRoom = api.CreateConference(HintConference);

            // Close the dialog afterwards
            this.Close();
        }

        private void linkCancel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }
    }
}
